﻿using System;
using System.Collections.Generic;
using System.Text;
using METODESA.Server.Data.Exceptions;
using NHibernate;

namespace METODESA.Server.Data.METODESA.Nhibernate
{
    public class BaseDao
    {
        
   
        protected static ISession myISession;
        
        public static void saveEntity(object entity)
        {
            ITransaction myTransaction = null;

            try
            {
                myISession = NHibernateHelper.GetCurrentSession();
                myTransaction = myISession.BeginTransaction();
                myISession.Save(entity);
                myISession.Flush();
                myTransaction.Commit();
            }
            catch (HibernateException)
            {
                if (myTransaction != null) myTransaction.Rollback();
                NHibernateHelper.GetCurrentSession().Clear();
                throw;
            }
               

        }

        public static void updateEntity(object entity)
        {
            ITransaction myTransaction = null;
            try
            {
                myISession = NHibernateHelper.GetCurrentSession();
                myTransaction = myISession.BeginTransaction();
                myISession.Update(entity);
                myISession.Flush();
                myTransaction.Commit();
            }
            catch (HibernateException e)
            {
                if (myTransaction != null) 
                myTransaction.Rollback();
                myISession.Clear();
                throw e;
            }
        }
        public static void updateEntity(object parent,object child)
        {
            ITransaction myTransaction = null;
            try
            {
                myISession = NHibernateHelper.GetCurrentSession();
                myTransaction = myISession.BeginTransaction();
                myISession.Save(child);
                myISession.Update(parent);
                myISession.Flush();
                myTransaction.Commit();
            }
            catch (HibernateException e)
            {
                myTransaction.Rollback();
                myISession.Clear();
                throw e;
            }
        }

        public static void updateSimple(object entity)
    {
            try
            {
                myISession.Update(entity);
                myISession.Flush();
            }
            catch(HibernateException e)
            {
                myISession.Clear();
                throw e;

            }

               
        
    }
        public static void deleteSimple(object entity)
        {
            try
            {
                myISession.Delete(entity);
                myISession.Flush();
            }
            catch (HibernateException e)
            {
                myISession.Clear();
                throw e;

            }

        }
        public static void deleteEntity(object entity)
        {
            ITransaction myTransaction = null;

            try
            {
                myISession = NHibernateHelper.GetCurrentSession();
                myTransaction = myISession.BeginTransaction();
                myISession.Delete(entity);
                myISession.Flush();
                myTransaction.Commit();
            }
            catch (HibernateException e)
            {
                myTransaction.Rollback();
                myISession.Clear();
                throw e;
            }
          
        }

        public static void saveSimple(object entity)
        {
            try
            {
                myISession.Save(entity);
                myISession.Flush();
            }
            catch (HibernateException e)
            {
                myISession.Clear();
                throw  e;

            }

        }
    
    }
}
